package com.tang.server;

import com.tang.msg.Request;
import com.tang.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * 服务端消息处理
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2022/5/19 17:55
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Request request = (Request) msg;
        // 反馈
        Response response = new Response();
        response.setRequestId(request.getRequestId());
        response.setParam(request.getResult() + "请求成功，反馈结果请接收处理");
        ctx.writeAndFlush(response);
        // 释放
        ReferenceCountUtil.release(request);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
    
}
